home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / ADA / GNAT / !gcc / adainc / 6 / adb / s-wchwts < prev    next >
Text File  |  1996-02-12  |  6KB  |  138 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                       S Y S T E M . W C H _ W T S                        --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.9 $                              --
  10. --                                                                          --
  11. --   Copyright (C) 1992,1993,1994,1995,1996 Free Software Foundation, Inc.  --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNAT was originally developed  by the GNAT team at  New York University. --
  32. -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
  33. --                                                                          --
  34. ------------------------------------------------------------------------------
  35.  
  36. with System.WCh_Con; use System.WCh_Con;
  37. with System.WCh_JIS; use System.WCh_JIS;
  38.  
  39. package body System.WCh_WtS is
  40.  
  41.    ---------------------------
  42.    -- Wide_String_To_String --
  43.    ---------------------------
  44.  
  45.    function Wide_String_To_String
  46.      (S    : Wide_String;
  47.       EM   : WC_Encoding_Method)
  48.       return String
  49.    is
  50.       R  : String (1 .. 5 * S'Length); -- worst case length!
  51.       RP : Natural;
  52.       B1 : Natural;
  53.       B2 : Natural;
  54.       C1 : Character;
  55.       C2 : Character;
  56.  
  57.    begin
  58.       RP := 0;
  59.  
  60.       for SP in S'Range loop
  61.          declare
  62.             C   : constant Wide_Character := S (SP);
  63.             CV  : constant Natural        := Wide_Character'Pos (C);
  64.             Hex : constant array (0 .. 15) of Character := "0123456789ABCDEF";
  65.  
  66.          begin
  67.             if CV <= 127
  68.               or else (CV <= 255 and then EM = WCEM_Hex)
  69.             then
  70.                RP := RP + 1;
  71.                R (RP) := Character'Val (CV);
  72.  
  73.             else
  74.                B1 := CV / 256;
  75.                B2 := CV mod 256;
  76.  
  77.                --  Hex ESC sequence encoding
  78.  
  79.                if EM = WCEM_Hex then
  80.                   R (RP + 1) := Ascii.ESC;
  81.                   R (RP + 2) := Hex (B1 / 16);
  82.                   R (RP + 3) := Hex (B1 rem 16);
  83.                   R (RP + 4) := Hex (B2 / 16);
  84.                   R (RP + 5) := Hex (B2 rem 16);
  85.                   RP := RP + 5;
  86.  
  87.                --  Upper bit shift (internal code = external code)
  88.  
  89.                elsif EM = WCEM_Upper then
  90.                   C1 := Character'Val (B1);
  91.                   C2 := Character'Val (B2);
  92.  
  93.                --  Upper bit shift (EUC)
  94.  
  95.                elsif EM = WCEM_EUC then
  96.                   JIS_To_EUC (C, C1, C2);
  97.  
  98.                --  Upper bit shift (Shift-JIS)
  99.  
  100.                elsif EM = WCEM_Shift_JIS then
  101.                   JIS_To_Shift_JIS (C, C1, C2);
  102.  
  103.                --  Brackets encoding (also used in default no encoding mode)
  104.  
  105.                else -- EM = WCEM_Brackets or else EM = WCEM_None
  106.                   R (RP + 1) := '[';
  107.                   R (RP + 2) := '"';
  108.  
  109.                   if B1 = 0 then
  110.                      R (RP + 3) := Hex (B2 / 16);
  111.                      R (RP + 4) := Hex (B2 rem 16);
  112.                      R (RP + 5) := '"';
  113.                      R (RP + 6) := ']';
  114.                      RP := RP + 6;
  115.  
  116.                   else
  117.                      R (RP + 3) := Hex (B1 / 16);
  118.                      R (RP + 4) := Hex (B1 rem 16);
  119.                      R (RP + 5) := Hex (B2 / 16);
  120.                      R (RP + 6) := Hex (B2 rem 16);
  121.                      R (RP + 7) := '"';
  122.                      R (RP + 8) := ']';
  123.                      RP := RP + 8;
  124.                   end if;
  125.                end if;
  126.  
  127.                R (RP + 1) := C1;
  128.                R (RP + 2) := C2;
  129.                RP := RP + 2;
  130.             end if;
  131.          end;
  132.       end loop;
  133.  
  134.       return R (1 .. RP);
  135.    end Wide_String_To_String;
  136.  
  137. end System.WCh_WtS;
  138.